package com.anysoft;

import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.anysoft.api.OkGoService;
import com.anysoft.utils.DiskUtils;
import com.anysoft.utils.RCrashHandler;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsDownloader;
import com.tencent.smtt.sdk.TbsListener;
import com.zhy.autolayout.config.AutoLayoutConifg;

import java.io.File;
import java.util.concurrent.ConcurrentHashMap;

import ren.yale.android.cachewebviewlib.CacheWebView;

/**
 * Created by wwq on 2016/12/7.
 */

public class YouthLifeApplication extends MultiDexApplication {
    private final static String TAG = "MyApp";
    public static Context context;
    
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    
    
    @Override
    public void onCreate() {
        super.onCreate();
        context = this.getApplicationContext();
        initThreeLib();
    }
    
    private void initThreeLib() {
        OkGoService.init(this);
        AutoLayoutConifg.getInstance().useDeviceSize();
        //初始化Fresco
        Fresco.initialize(getApplicationContext());
        initTBS();
        //初始化下载配置
        initCrashHandler();
        File cacheFile = new File(this.getCacheDir(), "icbc_caches");

        CacheWebView.getCacheConfig().init(this, cacheFile.getAbsolutePath(), 1024 * 1024 * 100, 1024 * 1024 * 10)
                .enableDebug(true);//100M 磁盘缓存空间,10M 内存缓存空间
    }
    
    private void initCrashHandler() {
        RCrashHandler.CrashUploader mCrashUploader = new RCrashHandler.CrashUploader() {
            @Override
            public void uploadCrashMessage(ConcurrentHashMap<String, Object> infos) {
            }
        };
        RCrashHandler.getInstance(DiskUtils.CACHE_LOG_PATH).init(this, mCrashUploader);
    }
    
    private void initTBS() {
    
        QbSdk.PreInitCallback preInitCallback = new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                Log.e("tbs", "onCoreInitFinished: ");
            }
        
            @Override
            public void onViewInitFinished(boolean b) {
                Log.e("tbs", "onViewInitFinished: " + b);
            }
        };
    
        //tbs内核下载跟踪
        TbsListener tbsListener = new TbsListener() {
            @Override
            public void onDownloadFinish(int i) {
                //tbs内核下载完成回调
                Log.e("tbs", "内核下载完成回调 onDownloadFinish: " + i);
            }
        
            @Override
            public void onInstallFinish(int i) {
                //内核安装完成回调，
                Log.e("tbs", "内核安装完成回调 onInstallFinish: " + i);
            }
        
            @Override
            public void onDownloadProgress(int i) {
                //下载进度监听
                Log.e("tbs", "下载进度监听 onDownloadProgress: " + i);
            }
        };
        //初始化方式可以用这种,也可以用下面的这种 QbSdk.initX5Environment(this,preInitCallback);
//        QbSdk.preInit(this, preInitCallback);
        QbSdk.initX5Environment(this, preInitCallback);
        //tbs内核下载跟踪
        QbSdk.setTbsListener(tbsListener);
        //判断是否要自行下载内核
        QbSdk.setDownloadWithoutWifi(true);
        boolean needDownload = TbsDownloader.needDownload(this, TbsDownloader.DOWNLOAD_OVERSEA_TBS);
        if (needDownload) {
            TbsDownloader.startDownload(this);
        }
    }
}
